<script setup lang="ts">
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { status } from "nprogress";
import { ref, reactive } from "vue";

import Upload from "@iconify-icons/ri/upload-line";
import Role from "@iconify-icons/ri/admin-line";
import Password from "@iconify-icons/ri/lock-password-line";
import More from "@iconify-icons/ep/more-filled";
import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import Refresh from "@iconify-icons/ep/refresh";
import AddFill from "@iconify-icons/ri/add-circle-line";

import { useManage } from "./utils/hook";
import { usePublicHooks } from "../../hooks";
const { buttonClass } = usePublicHooks();

/** 搜索区DOM */
const formRef = ref(null);
/** 表格DOM */
const tableRef = ref();

const {
  dataList,
  shoppingidList,
  manageSearch,
  selectedNum,
  columns,
  loading,
  pagination,
  resetForm,
  onSearch,
  openDialog,
  onSelectionCancel,
  onbatchDel,
  handleSelectionChange,
  handleDelete,
  handleUpdate,
  handleReset,
  handleSizeChange,
  handleCurrentChange
} = useManage(tableRef);
</script>

<template>
  <div>
    <div class="container mx-auto">
      <!-- 用户搜索表单 -->
      <el-form
        ref="formRef"
        :inline="true"
        :model="manageSearch"
        class="search-form bg-bg_color px-8 pt-[12px]"
      >
        <el-form-item label="模糊搜素：" prop="key">
          <el-input
            v-model="manageSearch.key"
            placeholder="请输入礼品名称或描述"
            clearable
            class="!w-[220px]"
          />
        </el-form-item>
        <el-form-item label="发货方式：" prop="shippingid">
          <el-select
            v-model="manageSearch.shippingid"
            placeholder="请选择"
            clearable
            class="!w-[180px]"
          >
            <el-option
              v-for="(item, index) in shoppingidList"
              :key="index"
              :label="item.name"
              :value="item.shippingid"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :icon="useRenderIcon('ri:search-line')"
            @click="onSearch"
          >
            搜索
          </el-button>
          <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <PureTableBar title="礼品管理" :columns="columns" @refresh="onSearch">
        <!-- 表头右侧按钮 -->
        <template #buttons>
          <el-button
            type="primary"
            :icon="useRenderIcon(AddFill)"
            @click="openDialog()"
          >
            新增礼品
          </el-button>
        </template>

        <!-- dynamicColumns 动态列 -->
        <template v-slot="{ size, dynamicColumns }">
          <!-- 批量操作【默认隐藏】 -->
          <div
            v-if="selectedNum > 0"
            v-motion-fade
            class="bg-[var(--el-fill-color-light)] w-full h-[46px] mb-2 pl-4 flex items-center"
          >
            <!-- 多选数量提示 -->
            <div class="flex-auto">
              <span
                style="font-size: var(--el-font-size-base)"
                class="text-[rgba(42,46,54,0.5)] dark:text-[rgba(220,220,242,0.5)]"
              >
                已选 {{ selectedNum }} 项
              </span>
              <!-- @click="" -->
              <el-button type="primary" text @click="onSelectionCancel">
                取消选择
              </el-button>
            </div>
            <!-- 批量删除提示 -->
            <el-popconfirm title="是否确认删除?" @confirm="onbatchDel">
              <template #reference>
                <el-button type="danger" text class="mr-1">
                  批量删除
                </el-button>
              </template>
            </el-popconfirm>
          </div>

          <!-- 表格内容区域 -->
          <pure-table
            ref="tableRef"
            row-key="id"
            :loading="loading"
            adaptive
            :size="size"
            :adaptiveConfig="{ offsetBottom: 108 }"
            align-whole="center"
            table-layout="auto"
            :data="dataList"
            :columns="dynamicColumns"
            :header-cell-style="{
              background: 'var(--el-fill-color-light)',
              color: 'var(--el-text-color-primary)'
            }"
            @selection-change="handleSelectionChange"
          >
            <!-- 【操作】表格按钮操作区域 -->
            <template #operation="{ row }">
              <!-- 【操作】 修改按钮 -->
              <el-button
                class="reset-margin"
                link
                type="primary"
                :size="size"
                :icon="useRenderIcon(EditPen)"
                @click="openDialog('修改', row)"
              >
                修改
              </el-button>
              <!-- 删除提示信息 -->
              <el-popconfirm
                :title="`是否删除用户编号:${row.goodsid}这条数据`"
                @confirm="handleDelete(row)"
              >
                <template #reference>
                  <el-button
                    class="reset-margin"
                    link
                    type="primary"
                    :size="size"
                    :icon="useRenderIcon(Delete)"
                  >
                    删除
                  </el-button>
                </template>
              </el-popconfirm>
            </template>
          </pure-table>
        </template>
      </PureTableBar>
    </div>
  </div>
</template>

<style scoped>
.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
